package com.hjm.project.gears_info.controller;

import java.util.List;
import java.util.Map;

import com.hjm.project.gears_info.service.IGearsInfoService;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.gears_info.domain.GearsInfo;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 服务商档位信息（分润标准）Controller
 * 
 * @author LOVE
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/gears_info/gears_info")
public class GearsInfoController extends BaseController
{
    private String prefix = "gears_info/gears_info";

    @Autowired
    private IGearsInfoService gearsInfoService;

    @Autowired
    private IManufacturerService manufacturerService;

    @RequiresPermissions("gears_info:gears_info:view")
    @GetMapping()
    public String gears_info()
    {
        return prefix + "/gears_info";
    }

    /**
     * 查询服务商档位信息（分润标准）列表
     */
    @RequiresPermissions("gears_info:gears_info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GearsInfo gearsInfo)
    {
        startPage();
        List<GearsInfo> list = gearsInfoService.selectGearsInfoList(gearsInfo);
        return getDataTable(list);
    }

    /**
     * 导出服务商档位信息（分润标准）列表
     */
    @RequiresPermissions("gears_info:gears_info:export")
    @Log(title = "服务商档位信息（分润标准）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GearsInfo gearsInfo)
    {
        List<GearsInfo> list = gearsInfoService.selectGearsInfoList(gearsInfo);
        ExcelUtil<GearsInfo> util = new ExcelUtil<GearsInfo>(GearsInfo.class);
        return util.exportExcel(list, "gears_info");
    }

    /**
     * 新增服务商档位信息（分润标准）
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务商档位信息（分润标准）
     */
    @RequiresPermissions("gears_info:gears_info:add")
    @Log(title = "服务商档位信息（分润标准）", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam Map param, GearsInfo gearsInfo) throws Exception {
        Long startMoney = Long.parseLong(String.valueOf(param.get("startMoney")));
        Long endMoney = Long.parseLong(String.valueOf(param.get("endMoney")));
        if (startMoney  > endMoney) {
            return error("起始金额不能大于结束金额");
        }
        gearsInfo.setPaySection(startMoney + "-" + endMoney);
        gearsInfo.setGrade("V"+gearsInfo.getGrade());
        return toAjax(gearsInfoService.insertGearsInfo(gearsInfo));
    }

    /**
     * 修改服务商档位信息（分润标准）
     */
    @GetMapping("/edit/{grade}")
    public String edit(@PathVariable("grade") String grade, ModelMap mmap)
    {
        GearsInfo gearsInfo = gearsInfoService.selectGearsInfoById(grade);
        String[] split = gearsInfo.getPaySection().split("-");
        gearsInfo.setGrade(gearsInfo.getGrade().replace("V",""));
        mmap.put("gearsInfo", gearsInfo);
        mmap.put("startMoney",split[0]);
        mmap.put("endMoney",split[1]);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务商档位信息（分润标准）
     */
    @RequiresPermissions("gears_info:gears_info:edit")
    @Log(title = "服务商档位信息（分润标准）", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam Map param, GearsInfo gearsInfo)
    {
        gearsInfo.setPaySection(param.get("startMoney") + "-" + param.get("endMoney"));
        gearsInfo.setGrade("V" + gearsInfo.getGrade());

        return toAjax(gearsInfoService.updateGearsInfo(gearsInfo));
    }

    /**
     * 删除服务商档位信息（分润标准）
     */
    @RequiresPermissions("gears_info:gears_info:remove")
    @Log(title = "服务商档位信息（分润标准）", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gearsInfoService.deleteGearsInfoByIds(ids));
    }


    /**
     * 配置达标页面
     */
    @GetMapping("/dbfx")
    public String dbfx() {
        return prefix + "/dbfx";
    }


    /**
     * 厂商查找带回
     */
    @GetMapping("/cs")
    public String cs() {
        return prefix + "/cs";
    }

    /**
     * 厂商查找带回
     */
    @GetMapping("/xh")
    public String xh(ModelMap map,@RequestParam String manu_code) {
        map.put("manu_code",manu_code);
        return prefix + "/xh";
    }


    /**
     * 获取所有机具
     * @return
     */
    @Log(title = "终端信息")
    @PostMapping("/getAll")
    @ResponseBody
    public TableDataInfo getAll(Map map) {
        return getDataTable(manufacturerService.getAll(map));
    }



}
